package com.example.myapplication.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.R;
import com.example.myapplication.model.Password;

import java.util.List;

public class PasswordAdapter extends RecyclerView.Adapter<PasswordAdapter.PasswordViewHolder> {
    private List<Password> passwords;
    private OnPasswordClickListener listener;

    public interface OnPasswordClickListener {
        void onPasswordClick(Password password);
    }

    public void setOnPasswordClickListener(OnPasswordClickListener listener) {
        this.listener = listener;
    }

    public void setPasswords(List<Password> passwords) {
        this.passwords = passwords;
    }

    @NonNull
    @Override
    public PasswordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_password, parent, false);
        return new PasswordViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PasswordViewHolder holder, int position) {
        Password password = passwords.get(position);
        holder.tvWebsite.setText(password.getWebsite());
        holder.tvUsername.setText(password.getUsername());
        holder.tvPassword.setText(password.getPassword());

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onPasswordClick(password);
            }
        });
    }

    @Override
    public int getItemCount() {
        return passwords != null ? passwords.size() : 0;
    }

    static class PasswordViewHolder extends RecyclerView.ViewHolder {
        TextView tvWebsite;
        TextView tvUsername;
        TextView tvPassword;

        PasswordViewHolder(@NonNull View itemView) {
            super(itemView);
            tvWebsite = itemView.findViewById(R.id.tvWebsite);
            tvUsername = itemView.findViewById(R.id.tvUsername);
            tvPassword = itemView.findViewById(R.id.tvPassword);
        }
    }
} 